<?php


namespace app\api\controller\v33;


use app\api\controller\v2\Auth;
use app\common\cachemodel\active\Active1CouponRecord;
use app\common\cachemodel\active\Active1SignIn;
use app\common\cachemodel\active\Active1UserHelp;
use app\common\cachemodel\active\Active1UserHelpRecord;
use app\common\cachemodel\AppConfig;
use app\common\cachemodel\UserCoupon;
use app\common\cachemodel\ZlCouponAttr;
use app\common\lib\SensorsData;
use app\common\lib\Util;
use app\common\model\Goods;
use think\Log;

class WebActive extends Auth
{
    public $activeFlag = 20220804;  // 七夕活动

    private $signInCoupon = [
        1 => [  // 第一天
            1 => [  //  1=>恋爱 2=>婚姻 3=>合盘
                499 => 0.9, // 88折    优惠券ID => 获得机率
                505 => 0.1, // 5折
                513 => 0,   // 免费
            ],
            2 => [
                500 => 0.9, // 88折    优惠券ID => 获得机率
                506 => 0.1, // 5折
                514 => 0,   // 免费
            ],
            3 => [
                501 => 0.9, // 88折    优惠券ID => 获得机率
                507 => 0.1, // 5折
                512 => 0,   // 免费
            ],
        ],
        3 => [  // 第三天
            511 => 0.9, // 年运77折    优惠券ID => 获得机率
            510 => 0.1, // 年运5折
            422 => 0,   // 年运免费
        ],
        5 => [  // 第五天
            509 => 0.9, // 一生报告77折    优惠券ID => 获得机率
            508 => 0.1, // 一生报告5折
            213 => 0,   // 一生报告免费
        ]
    ];

    private $helpCouponConfig = [
        1 => [
            1 => [
                'name' => '恋爱报告五折券',
                'help_num' => 17,
                'cid' => 505,
                'id' => 11,
            ],
            2 => [
                'name' => '恋爱报告免费券',
                'help_num' => 57,
                'cid' => 513,
                'id' => 12,
            ]
        ],
        2 => [
            1 => [
                'name' => '婚姻报告五折券',
                'help_num' => 17,
                'cid' => 506,
                'id' => 21,
            ],
            2 => [
                'name' => '婚姻报告免费券',
                'help_num' => 57,
                'cid' => 514,
                'id' => 22,
            ]
        ],
        3 => [
            1 => [
                'name' => '合盘报告五折券',
                'help_num' => 17,
                'cid' => 507,
                'id' => 31,
            ],
            2 => [
                'name' => '合盘报告免费券',
                'help_num' => 57,
                'cid' => 512,
                'id' => 32,
            ]
        ],
    ];

    private $activeTime = [
        'start_time' => 1659283200,  // 活动开始时间 1659283200    2022-08-01 00:00:00
        'end_time' => 1659715200,   // 2022-08-06 00:00:00 结束时间
    ];

    /**
     * 活动首页
     */
    public function index()
    {
        $appConfig = (new AppConfig())->configInfo('valentine2022');
        if ($appConfig && is_array($appConfig) && isset($appConfig['date']) && $appConfig['date'])
        {
            $date = (int)$appConfig['date'];
        } else
        {
            $date = (int)(date('Ymd'));
        }
        $nowTime = strtotime($date . ' ' . date('H:i:s'));

        if ($nowTime < $this->activeTime['start_time'])
        {
            $status = 0;
        } elseif ($nowTime >= $this->activeTime['end_time'])
        {
            $status = 2;
        } else
        {
            $status = 1;
        }

        $activeSignInModel = new Active1SignIn();

        $data = $activeSignInModel->getSignInData($this->activeFlag, $this->user['id']);
        $activeCouponRecordModel = new Active1CouponRecord();

        $giveCouponRecord = $activeCouponRecordModel->getCouponData($this->activeFlag, $this->user['id'], 1);
        $Active1UserHelpModel = new Active1UserHelp();
        $helpDetails1 = $Active1UserHelpModel->getUserHelpDetails($this->activeFlag, $this->user['id'], 1);
        $helpDetails2 = $Active1UserHelpModel->getUserHelpDetails($this->activeFlag, $this->user['id'], 2);
        $helpDetails3 = $Active1UserHelpModel->getUserHelpDetails($this->activeFlag, $this->user['id'], 3);
        if ($nowTime < $this->activeTime['start_time'])
        {
            $status1 = 0;
            $status2 = 0;
            $status3 = 0;
        } elseif ($nowTime >= $this->activeTime['end_time'])
        {
            $status1 = 2;
            $status2 = 2;
            $status3 = 2;
        } else
        {
            $status1 = isset($helpDetails1['end_time']) && $helpDetails1['end_time'] ? 2 : 1;
            $status2 = isset($helpDetails2['end_time']) && $helpDetails2['end_time'] ? 2 : 1;
            $status3 = isset($helpDetails3['end_time']) && $helpDetails3['end_time'] ? 2 : 1;
        }


        return show(config('code.success'), '请求成功', [
            'status' => $status,    // 状态 0=>活动未开始 1=>活动进行中 2=>活动已结束
            'sign_in_num' => $data ? count($data) : 0, // 已签到天数
            'give_coupon3' => $giveCouponRecord && isset($giveCouponRecord[3]) ? 1 : 0,    // 领取第三天优惠券 0=>未领取   1=>已领取
            'give_coupon5' => $giveCouponRecord && isset($giveCouponRecord[5]) ? 1 : 0,    // 领取第五天优惠券 0=>未领取   1=>已领取
            'is_sign_in' => $data && isset($data[$date]) ? 1 : 0,   // 今天是否签到 0=>未签到 1=>已签到
            'help_coupon' => [
                [
                    'type' => 1,    //  1=>恋爱 2=>婚姻 3=>合盘
                    'name' => '恋爱报告',
                    'help_num' => $helpDetails1['help_num'] ?? 0, // 已助力数
                    'status' => $status1, // 0=>活动未开始 1=>进行中   2=>已结束
                ], [
                    'type' => 2,    //  1=>恋爱 2=>婚姻 3=>合盘
                    'name' => '婚姻报告',
                    'help_num' => $helpDetails2['help_num'] ?? 0, // 已助力数
                    'status' => $status2,// 0=>活动未开始 1=>进行中   2=>已结束
                ], [
                    'type' => 3,    //  1=>恋爱 2=>婚姻 3=>合盘
                    'name' => '合盘报告',
                    'help_num' => $helpDetails3['help_num'] ?? 0, // 已助力数
                    'status' => $status3,// 0=>活动未开始 1=>进行中   2=>已结束
                ],
            ]
        ]);
    }

    /**
     * 签到
     */
    public function signIn()
    {
        $type = input('type/d', 0);

        $appConfig = (new AppConfig())->configInfo('valentine2022');

        if ($appConfig && is_array($appConfig) && isset($appConfig['date']) && $appConfig['date'])
        {
            $date = (int)$appConfig['date'];
        } else
        {
            $date = (int)(date('Ymd'));
        }
        $nowTime = strtotime($date . ' ' . date('H:i:s'));

        if ($nowTime < $this->activeTime['start_time'])
        {
            return show(config('code.error'), '活动未开始，签到失败～');
        }

        if ($nowTime >= $this->activeTime['end_time'])
        {
            return show(config('code.error'), '活动已结束，签到失败～');
        }

        $activeSignInModel = new Active1SignIn();

        $data = $activeSignInModel->getSignInData($this->activeFlag, $this->user['id']);

        if (isset($data[$date]))
        {
            return show(config('code.error'), '今天已经签到过了～');
        }
        try
        {
            $cInfo = null;
            if (empty($data))
            {   // 第一天 签到
                if (!in_array($type, [1, 2, 3], true))
                {
                    return show(config('code.error'), '请求异常，签到失败～');
                }
                $cid = $this->randomGetCid($this->signInCoupon[1][$type]);
                // 发放优惠券
                $userCouponModel = new UserCoupon();
                $couponInfo = (new ZlCouponAttr())->getInfoById($cid, ['desc', 'type', 'rand_min', 'rand_max', 'ur_id', 'gids', 'discount']);
                $cData = [
                    'uid' => $this->user['id'],
                    'c_id' => $cid,
                    'ur_id' => $couponInfo['ur_id'],
                    'start_time' => $this->request->time(),
                    'end_time' => strtotime("+7 day"),
                    'give_time' => $this->request->time(),
                    'status' => 0,
                    'price' => $couponInfo['type'] == 4 ? mt_rand($couponInfo['rand_min'], $couponInfo['rand_max']) : 0,
                    'is_give' => 0,
                    'create_time' => $this->request->time(),
                    'update_time' => 0,
                ];
                $ucId = $userCouponModel->insertGetId($cData);
                $ids[$ucId] = $ucId; // 添加未用于ID
                // 神策打点
                $gidsArr = explode(',', $couponInfo['gids']);
                $couponRangeArr = (new Goods())->where('id', 'in', $gidsArr)->column('name');
                // 神策打点 75 获得优惠券 AcquireCoupon
                SensorsData::track($this->user['id'], true, 'AcquireCoupon', [
                    'refer_name' => '',
                    'refer_link' => '',
                    'coupon_name' => $couponInfo['desc'],
                    'coupon_expire' => date('Y-m-d H:i:s', $cData['end_time']),
                    'coupon_type' => $userCouponModel->couponTypeName[$couponInfo['type']],
                    'coupon_range' => $couponRangeArr,
                    'acquire_channel' => '2022七夕活动',
                    'is_give' => false,
                    'acquire_scene' => '签到1天',
                ]);
                // 写入 活动 获得优惠券记录
                (new Active1CouponRecord())->SaveData([
                    'active_flag' => $this->activeFlag,
                    'uid' => $this->user['id'],
                    'type' => 1,
                    'flag' => 1,   // 代表第几天
                    'cid' => $ucId,
                    'create_time' => $this->request->time(),
                ]);
                // 未使用
                $userCouponModel->listDataAdd($this->user['id'], $ids, 1);

                $cDesc = [
                    1 => '恋爱报告',
                    2 => '婚姻报告',
                    3 => '合盘报告',
                ];

                $cInfo = [
                    'name' => $this->getCouponTypeName($couponInfo),
                    'desc' => $cDesc[$type]
                ];
            }
            // 记录 签到 信息
            $res = $activeSignInModel->signIn($this->activeFlag, $this->user['id'], $date);

            if ($res)
            {
                $signDay = count($data) + 1;
                // 神策打点 232 22年七夕活动签到 ValentineSignIn2022
                SensorsData::track($this->user['id'], true, 'ValentineSignIn2022', ['sign_day' => $signDay]);

                return show(config('code.success'), '签到成功', [
                    'coupon_info' => $cInfo
                ]);
            } else
            {
                return show(config('code.error'), '服务异常，请稍后再试～');
            }
        }
        catch (\Exception $e)
        {
            Log::write('WebActive model => signIn()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');

            return show(config('code.error'), '服务异常，请稍后再试～');
        }
    }

    /**
     * 根据概率获取优惠券ID
     * @param array $couponData
     * @return int
     * @throws \Exception
     */
    protected function randomGetCid(array $couponData): int
    {
        $num = random_int(1, 10000000);
        $resScale = 0;

        foreach ($couponData as $cid => $percent)
        {
            $resScale = $resScale + $percent * 10000000;  // 此次因为要除以100  所以直接*的时候少两个0

            if ($num <= $resScale)
            {   // 中奖了
                return $cid;
            }
        }

        return 0;
    }

    /**
     * 获取 优惠券 折扣类型名称
     * @param array $couponInfo
     * @return string
     */
    protected function getCouponTypeName(array $couponInfo): string
    {
        $name = '';
        $couponInfo['type'] = (int)$couponInfo['type'];
        switch ($couponInfo['type'])
        { // 1折扣  2立减劵 3满减 4随机卷 5免费卷
            case 1:
                $numText = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
                $discount = sprintf('%02d', $couponInfo['discount']);
                $name = $numText[$discount[0]];
                if ($discount[1] > 0)
                {
                    $name .= $numText[$discount[1]];
                }
                $name .= '折';
                break;
            case 2:
                $name = '免费券';
                break;
            case 3:
                $name = '满减券';
                break;
            case 4:
                $name = '随机券';
                break;
            default:
                $name = '免费券';
        }
        return $name;
    }

    /**
     * 领取 签到 优惠券
     */
    public function getSignInCoupon()
    {
        $day = input('day/d', 0);
        if (!in_array($day, [3, 5], true))
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }

        $appConfig = (new AppConfig())->configInfo('valentine2022');

        if ($appConfig && is_array($appConfig) && isset($appConfig['date']) && $appConfig['date'])
        {
            $date = (int)$appConfig['date'];
        } else
        {
            $date = (int)(date('Ymd'));
        }
        $nowTime = strtotime($date . ' ' . date('H:i:s'));

        if ($nowTime < $this->activeTime['start_time'])
        {
            return show(config('code.error'), '活动未开始，领取失败～');
        }

        if ($nowTime >= $this->activeTime['end_time'])
        {
            return show(config('code.error'), '活动已结束，领取失败～');
        }

        $activeSignInModel = new Active1SignIn();

        $data = $activeSignInModel->getSignInData($this->activeFlag, $this->user['id']);

        if (count($data) < $day)
        {
            return show(config('code.error'), '领取条件不满足，请下次再试～');
        }

        $activeCouponRecordModel = new Active1CouponRecord();

        $giveCouponRecord = $activeCouponRecordModel->getCouponData($this->activeFlag, $this->user['id'], 1);

        if (isset($giveCouponRecord[$day]))
        {
            return show(config('code.error'), '优惠券已经领取过了');
        }

        // 发放优惠券
        $cid = $this->randomGetCid($this->signInCoupon[$day]);
        // 发放优惠券
        $userCouponModel = new UserCoupon();
        $couponInfo = (new ZlCouponAttr())->getInfoById($cid, ['desc', 'type', 'rand_min', 'rand_max', 'ur_id', 'gids', 'discount']);
        $cData = [
            'uid' => $this->user['id'],
            'c_id' => $cid,
            'ur_id' => $couponInfo['ur_id'],
            'start_time' => $this->request->time(),
            'end_time' => strtotime("+7 day"),
            'give_time' => $this->request->time(),
            'status' => 0,
            'price' => $couponInfo['type'] == 4 ? mt_rand($couponInfo['rand_min'], $couponInfo['rand_max']) : 0,
            'is_give' => 0,
            'create_time' => $this->request->time(),
            'update_time' => 0,
        ];
        $ucId = $userCouponModel->insertGetId($cData);
        $ids[$ucId] = $ucId; // 添加未用于ID
        // 神策打点
        $gidsArr = explode(',', $couponInfo['gids']);
        $couponRangeArr = (new Goods())->where('id', 'in', $gidsArr)->column('name');
        // 神策打点 75 获得优惠券 AcquireCoupon
        SensorsData::track($this->user['id'], true, 'AcquireCoupon', [
            'refer_name' => '',
            'refer_link' => '',
            'coupon_name' => $couponInfo['desc'],
            'coupon_expire' => date('Y-m-d H:i:s', $cData['end_time']),
            'coupon_type' => $userCouponModel->couponTypeName[$couponInfo['type']],
            'coupon_range' => $couponRangeArr,
            'acquire_channel' => '2022七夕活动',
            'is_give' => false,
            'acquire_scene' => "签到{$day}天",
        ]);
        // 写入 活动 获得优惠券记录
        (new Active1CouponRecord())->SaveData([
            'active_flag' => $this->activeFlag,
            'uid' => $this->user['id'],
            'type' => 1,
            'flag' => $day,   // 1 3 5 代表 第 1天｜第三天｜第五天
            'cid' => $ucId,
            'create_time' => $this->request->time(),
        ]);
        // 未使用
        $userCouponModel->listDataAdd($this->user['id'], $ids, 1);

        $cDesc = [
            3 => '年运',
            5 => '一生报告',
        ];

        return show(config('code.success'), '领取成功', [
            'coupon_info' => [
                'name' => $this->getCouponTypeName($couponInfo),
                'desc' => $cDesc[$day]
            ]
        ]);
    }

    /**
     * 获取 助力 详情
     * @throws \Exception
     */
    public function getHelpDetail()
    {
        $type = input('type/d', 0);  //  1恋爱 2婚姻 3合盘

        if (!in_array($type, [1, 2, 3], true))
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }

        $helpDetails = (new Active1UserHelp())->getUserHelpDetails($this->activeFlag, $this->user['id'], $type);

        $helpList = (new Active1UserHelpRecord())->getHelpList($helpDetails['id']);

        $couponInfo = $this->helpCouponConfig[$type];

        $appConfig = (new AppConfig())->configInfo('valentine2022');

        if ($appConfig && is_array($appConfig) && isset($appConfig['date']) && $appConfig['date'])
        {
            $date = (int)$appConfig['date'];
        } else
        {
            $date = (int)(date('Ymd'));
        }
        $nowTime = strtotime($date . ' ' . date('H:i:s'));

        if ($appConfig && is_array($appConfig))
        {
            $key1 = 'help_num' . $type . '_1';
            $key2 = 'help_num' . $type . '_2';
            if (isset($appConfig[$key1]) && $appConfig[$key1])
            {
                $couponInfo[1]['help_num'] = $appConfig[$key1];
            }
            if (isset($appConfig[$key2]) && $appConfig[$key2])
            {
                $couponInfo[2]['help_num'] = $appConfig[$key2];
            }
        }

        $typeTitleConfig = [
            1 => '助力拿恋爱报告免费券',
            2 => '助力拿婚姻报告免费券',
            3 => '助力拿合盘报告免费券',
        ];

        if ($nowTime < $this->activeTime['start_time'])
        {
            $status = 0;
        } elseif ($nowTime >= $this->activeTime['end_time'])
        {
            $status = 2;
        } else
        {
            if ($helpDetails['end_time'])
            {
                $status = 2;
            } else
            {
                $status = 1;
            }
        }

        return show(config('code.success'), '请求成功', [
            'title' => $typeTitleConfig[$type],
            'status' => $status, // 0=>活动未开始 1=>进行中   2=>已结束
            'help_id' => $helpDetails['id'],    // 助力ID
            'help_num' => $helpDetails['help_num'], // 当前助力数
            'coupon_info' => [
                [
                    'id' => $couponInfo[1]['id'],
                    'name' => $couponInfo[1]['name'],
                    'help_num' => $couponInfo[1]['help_num'],
                    'status' => $helpDetails['cid_type'] == 1 ? 1 : 0   // 0=>未领取 1=>已领取
                ], [
                    'id' => $couponInfo[2]['id'],
                    'name' => $couponInfo[2]['name'],
                    'help_num' => $couponInfo[2]['help_num'],
                    'status' => $helpDetails['cid_type'] == 2 ? 1 : 0
                ]
            ],
            'help_list' => $helpList,   // 助力记录 列表
        ]);
    }

    /**
     * 助力用户
     */
    public function helpUser()
    {
        $helpId = trim(input('help_id/s', ''));
        if (!$helpId)
        {
            return show(config('code.error'), '请求异常，助力失败～');
        }
        $appConfig = (new AppConfig())->configInfo('valentine2022');

        if ($appConfig && is_array($appConfig) && isset($appConfig['date']) && $appConfig['date'])
        {
            $date = (int)$appConfig['date'];
        } else
        {
            $date = (int)(date('Ymd'));
        }
        $nowTime = strtotime($date . ' ' . date('H:i:s'));

        if ($nowTime < $this->activeTime['start_time'])
        {
            return show(config('code.error'), '活动未开始，助力失败～');
        }
        if ($nowTime >= $this->activeTime['end_time'])
        {
            return show(config('code.error'), '活动已结束，助力失败～');
        }

        $Active1UserHelpModel = new Active1UserHelp();
        $Active1UserHelpRecordModel = new Active1UserHelpRecord();
        // 您助力的好友已经完成助力活动
        $helpDetails = $Active1UserHelpModel->getInfoById($helpId);
        if (!$helpDetails)
        {
            return show(config('code.error'), '助力邀请码错误，助力失败～');
        }
        if ($this->user['id'] == $helpDetails['uid'])
        {
            return show(config('code.error'), '不能为自己助力，助力失败～');
        }
        if ($helpDetails['end_time'])
        {
            return show(config('code.error'), '您助力的好友已经完成助力活动');
        }
        $userHelpIds = $Active1UserHelpRecordModel->getUserHelpIds($this->activeFlag, $this->user['id']);
        if ($userHelpIds)
        {
            // 您已帮助好友助力，不能重复助力
            if (isset($userHelpIds[$helpId]))
            {
                return show(config('code.error'), '您已帮助好友助力，不能重复助力');
            }
            $userHelpIdsArr = array_values($userHelpIds);
            // 今日助力次数不足，请明日再来
            if (in_array($date, $userHelpIdsArr, false))
            {
                return show(config('code.error'), '今日助力次数不足，请明日再来');
            }
        }
        // 助力数增加
        $Active1UserHelpModel->UpdateData($helpId, ['help_num' => $helpDetails['help_num'] + 1]);
        // 添加助力记录
        $Active1UserHelpRecordModel->SaveData([
            'active_flag' => $helpDetails['active_flag'],
            'help_uid' => $helpDetails['uid'],
            'uid' => $this->user['id'],
            'help_id' => $helpId,
            'date' => $date,
            'create_time' => $this->request->time(),
        ]);

        $cDesc = [
            1 => '恋爱',
            2 => '婚姻',
            3 => '合盘',
        ];
        // 神策打点 235 助力成功 ValentineHelpSuccess2022
        SensorsData::track($this->user['id'], true, 'ValentineHelpSuccess2022', [
            'help_uid' => $helpDetails['uid'],
            'coupon_type' => $cDesc[$helpDetails['flag']],
        ]);

        return show(config('code.success'), '助力成功');
    }

    /**
     * 获取助力优惠券
     */
    public function getHelpCoupon()
    {
        $id = input('id/d', 0);
        if (!in_array($id, [11, 12, 21, 22, 31, 32], true))
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }
        $Active1UserHelpModel = new Active1UserHelp();
        $activeCouponRecordModel = new Active1CouponRecord();

        $giveCouponRecord = $activeCouponRecordModel->getCouponData($this->activeFlag, $this->user['id'], 2);

        if (isset($giveCouponRecord[$id]))
        {
            return show(config('code.error'), '优惠券已经领取过了');
        }
        $appConfig = (new AppConfig())->configInfo('valentine2022');

        if ($appConfig && is_array($appConfig) && isset($appConfig['date']) && $appConfig['date'])
        {
            $date = (int)$appConfig['date'];
        } else
        {
            $date = (int)(date('Ymd'));
        }
        $nowTime = strtotime($date . ' ' . date('H:i:s'));

        if ($nowTime < $this->activeTime['start_time'])
        {
            return show(config('code.error'), '活动未开始，领取失败～');
        }
        if ($nowTime >= $this->activeTime['end_time'])
        {
            return show(config('code.error'), '活动已结束，领取失败～');
        }
        $t = (int)($id / 10);
        $t1 = $id % 10; // 领取优惠券  1=>五折券 2=>免费券
        $helpDetails = $Active1UserHelpModel->getUserHelpDetails($this->activeFlag, $this->user['id'], $t);

        if ($helpDetails['end_time'])
        {
            return show(config('code.error'), '助力领券活动已结束～');
        }

        $coupon = $this->helpCouponConfig[$t][$t1];

        if ($appConfig && is_array($appConfig))
        {
            $key1 = 'help_num' . $t . '_' . $t1;
            if (isset($appConfig[$key1]) && $appConfig[$key1])
            {
                $coupon['help_num'] = (int)$appConfig[$key1];
            }
            $key2 = 'help_coupon' . $t . '_' . $t1;
            if (isset($appConfig[$key2]) && $appConfig[$key2])
            {
                $coupon['cid'] = (int)$appConfig[$key2];
            }
        }

        if ($helpDetails['help_num'] < $coupon['help_num'])
        {
            return show(config('code.error'), '领取条件不足，领取失败～');
        }
        $cid = $coupon['cid'];
        // 发放优惠券
        $userCouponModel = new UserCoupon();
        $couponInfo = (new ZlCouponAttr())->getInfoById($cid, ['desc', 'type', 'rand_min', 'rand_max', 'ur_id', 'gids', 'discount']);
        $cData = [
            'uid' => $this->user['id'],
            'c_id' => $cid,
            'ur_id' => $couponInfo['ur_id'],
            'start_time' => $this->request->time(),
            'end_time' => strtotime("+7 day"),
            'give_time' => $this->request->time(),
            'status' => 0,
            'price' => $couponInfo['type'] == 4 ? mt_rand($couponInfo['rand_min'], $couponInfo['rand_max']) : 0,
            'is_give' => 0,
            'create_time' => $this->request->time(),
            'update_time' => 0,
        ];
        $ucId = $userCouponModel->insertGetId($cData);
        $ids[$ucId] = $ucId; // 添加未用于ID
        // 神策打点
        $gidsArr = explode(',', $couponInfo['gids']);
        $couponRangeArr = (new Goods())->where('id', 'in', $gidsArr)->column('name');
        // 神策打点 75 获得优惠券 AcquireCoupon
        SensorsData::track($this->user['id'], true, 'AcquireCoupon', [
            'refer_name' => '',
            'refer_link' => '',
            'coupon_name' => $couponInfo['desc'],
            'coupon_expire' => date('Y-m-d H:i:s', $cData['end_time']),
            'coupon_type' => $userCouponModel->couponTypeName[$couponInfo['type']],
            'coupon_range' => $couponRangeArr,
            'acquire_channel' => '2022七夕活动',
            'is_give' => false,
            'acquire_scene' => '助力完成',
        ]);
        // 写入 活动 获得优惠券记录
        (new Active1CouponRecord())->SaveData([
            'active_flag' => $this->activeFlag,
            'uid' => $this->user['id'],
            'type' => 2,
            'flag' => $id,   // 1 3 5 代表 第 1天｜第三天｜第五天
            'cid' => $ucId,
            'create_time' => $this->request->time(),
        ]);
        // 未使用
        $userCouponModel->listDataAdd($this->user['id'], $ids, 1);
        // 助力信息 记录优惠券领取信息
        $Active1UserHelpModel->UpdateData($helpDetails['id'], [
            'cid_type' => $t1,
            'cid' => $ucId,
            'end_time' => $this->request->time(),
        ]);

        $cDesc = [
            1 => '恋爱报告',
            2 => '婚姻报告',
            3 => '合盘报告',
        ];

        return show(config('code.success'), '领取成功', [
            'coupon_info' => [
                'name' => $this->getCouponTypeName($couponInfo),
                'desc' => $cDesc[$t]
            ]
        ]);
    }
}
